package com.design.observer.cup;


import com.design.observer.Observable;
import com.design.observer.Observers;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者-杯子
 * 属性：价格
 */
public class Cup implements Observable {
    // 数组：用于存放观察者
    private final List<Observers> list = new ArrayList();
    //杯子属性：价格
    private float price;

    public Cup() {
    }

    //构造方法
    public Cup(float price) {
        this.price = price;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
        //价格改变时通知观察者
        notifyObserver();
    }

    @Override
    public void register(Observers observers) {
        list.add(observers);
    }

    @Override
    public void remove(Observers observers) {
        list.remove(observers);
    }

    @Override
    public void notifyObserver() {
        for (Observers observers : list) {
            observers.update(price);
        }
    }
}